package com.cl.edu.service.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cl.core.orm.Page;
import com.cl.core.orm.SearchFilter;
import com.cl.core.orm.hibernate.HibernateWebUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.dao.LinksDAO;
import com.cl.edu.entity.Links;

@Service
@Transactional
public class LinksManager {
	@Autowired
	private LinksDAO linksDAO;
	
	/**
	 * 查单个链接
	 */
	public Links getLink(Integer id){
		return linksDAO.get(id);
	}
	
	/**
	 * 分页查询链接
	 * @param page
	 * @return
	 */
	public Page<Links> getLinkPage(Page<Links> page) {
		StringBuffer hql = new StringBuffer("from Links model");
		hql.append(" where 1=1 ");
		List<SearchFilter> filters = HibernateWebUtils.buildSearchFilters(
				Struts2Utils.getRequest(), "filter_");
		for (SearchFilter filter : filters) {
			if ("name".equals(filter.getPropertyName())) {
				hql.append(" and model.name like '%")
						.append(filter.getPropertyValue()).append("%'");
			} else if ("display".equals(filter.getPropertyName())) {
				hql.append(" and model.display =")
				.append(filter.getPropertyValue());
			}
		}
		hql.append(" order by model.").append(page.getOrderBy()).append(" ").append(page.getOrder());
		page = linksDAO.findPage(page, hql.toString());
		return page;
	}
	
	/**
	 * 增加链接
	 * @param link
	 */
	public void addLink(Links link){
		linksDAO.save(link);
	}
	
	/**
	 * 修改链接
	 * @param link
	 */
	public void updateLink(Links link){
		linksDAO.saveOrUpdate(link);
	}
	
	/**
	 * 删除链接
	 * @param id
	 */
	public void deleteLink(Integer id){
		linksDAO.delete(id);
	}

	

	
}
